﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Contract;
using WHC.OrderWater.Business;
using WHC.Hotel.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditShop : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditShop()
        {
            InitializeComponent();
        }

        private void SetInfo(ShopInfo info)
        {
            info.ShopName = txtName.Text;
            info.Address = txtAddress.Text;
            info.Note = txtNote.Text;
            info.Manager = txtManager.Text;
            info.Email = txtEmail.Text;
            info.Mobile = txtMobile.Text;
            info.Password = EncodeHelper.DesEncrypt(txtPassord.Text);
            info.Telephone1 = txtTelephone1.Text;
            info.Telephone2 = txtTelephone2.Text;
            info.Telephone3 = txtTelephone3.Text;
            info.Telephone4 = txtTelephone4.Text;
            info.Telephone5 = txtTelephone5.Text;
            info.LastUpdated = DateTime.Now;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("分店名称不能为空");
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                ShopInfo info = BLLFactory<Shop>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<Shop>.Instance.Update(info, info.ID);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                ShopInfo info = new ShopInfo();
                SetInfo(info);
                info.ID = Guid.NewGuid().ToString();

                try
                {
                    bool succeed = BLLFactory<Shop>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void FrmEditCustomer_Load(object sender, EventArgs e)
        {
            this.btnOK.Enabled = Portal.gc.HasFunction("BasicInfo/Shop");
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                ShopInfo info = BLLFactory<Shop>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtAddress.Text = info.Address;
                    txtName.Text = info.ShopName;
                    txtManager.Text = info.Manager;
                    txtMobile.Text = info.Mobile;
                    txtEmail.Text = info.Email;
                    txtPassord.Text = EncodeHelper.DesDecrypt(info.Password);
                    txtRePass.Text = txtPassord.Text;
                    txtNote.Text = info.Note;
                    txtTelephone1.Text = info.Telephone1;
                    txtTelephone2.Text = info.Telephone2;
                    txtTelephone3.Text = info.Telephone3;
                    txtTelephone4.Text = info.Telephone4;
                    txtTelephone5.Text = info.Telephone5;
                    lblCreateDate.Text = info.LastUpdated.ToString();
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }
    }
}
